<?php

namespace app\common\model\system;

use app\common\model\BaseModel;

/**
 * 系统管理员
 * Class SystemManager
 * @package app\common\model\system
 */
class SystemManagerModel extends BaseModel
{
    //
    protected $name = 'system_manager';
    // 序号
    public string $fieldId = 'id';
    // 登录名
    public string $fieldUsername = 'username';
    // 密码,
    public string $fieldPasswd = 'passwd';
    // 手机号码
    public string $fieldMobile = 'mobile';
    // 手机号码
    public string $fieldNickname = 'nickname';
    // 超级用户 1 超级用户,0普通管理员
    public string $fieldIsSuper = 'is_super';
    // 状态 1正常 0 禁用
    public string $fieldStatus = 'status';
    // 备注
    public string $fieldMemo = 'memo';

    // 设置字段信息
    protected $schema = [
        'id' => 'int',        // 序号
        'username' => 'string',        // 登录名
        'passwd' => 'string',        // 密码,
        'mobile' => 'string',        // 手机号码
        'nickname' => 'string',        // 手机号码
        'is_super' => 'int',        // 超级用户 1 超级用户,0普通管理员
        'status' => 'int',        // 状态 1正常 0 禁用
        'memo' => 'string',        // 备注
    ];

    /**
     *  用户业绩统计
     * @param $table |表
     * @param $time_where | 时间条件
     * @param $page
     * @param $page_size
     * @return array|mixed
     * @throws \think\db\exception\DbException
     */
    public function getAdminStatisticData($table ,$time_where, $page , $page_size)
    {
        $data = $this->db()->field('id,concat(username,\'|\',mobile) as username,status')
            ->paginate(['page' => $page ?: 1, 'list_rows' => $page_size ?: 15]);
        if (empty($data['data'])) {
            return [];
        }
        $merchant = $this->db($table)
            ->where($time_where ?? [])
            ->group('op_user')
            ->column('count(op_user),op_user,count(status) as status_sum,sum(recharge_money) as recharge_money,
            sum(account_money) as account_money,sum(manager_red_packet) as red_packet,sum(manager_point) as point,sum(code_number) as code_number,
            sum(case when type=2 then 1 else 0 end) as type, sum(case when status=2 then 1 else 0 end) as status', 'op_user');
        $ages = array();
        foreach ($data['data'] as  &$v) {
            $ages[] = $v['merchant_count'] = $merchant[$v['id']]['count'] ?? 0;
            $v['recharge_money'] = $merchant[$v['id']]['recharge_money'] ?? 0;
            $v['account_money'] = $merchant[$v['id']]['account_money'] ?? 0;
            $v['red_packet'] = $merchant[$v['id']]['red_packet'] ?? 0;
            $v['point'] = $merchant[$v['id']]['point'] ?? 0;
            $v['code_number'] = $merchant[$v['id']]['code_number'] ?? 0;
            $v['type_num'] = $merchant[$v['id']]['type_num'] ?? 0;
            $v['status_num'] = $merchant[$v['id']]['status_num'] ?? 0;
            $v['status_name'] = (int)$v['status'] ===1 ?'正常': '禁用';
            $v['statistics_date'] = $time_where['statistics_date'];
        }
        array_multisort($ages, SORT_DESC, $data['data']);
        return $data;
    }
}
